import { Component, OnInit, ViewChild } from '@angular/core';
import { NavController, IonRefresher, IonInfiniteScroll } from '@ionic/angular';
import { HttpService } from 'src/app/service/http.service';
import { ToolService } from 'src/app/service/tool.service';
import { UrlService } from 'src/app/service/url.service';
import { ConfigService } from 'src/app/service/config.service';
import { PopwinService } from 'src/app/service/popwin.service';
import { Storage } from '@ionic/storage';

@Component({
  selector: 'app-notice',
  templateUrl: './notice.page.html',
  styleUrls: ['./notice.page.scss'],
})
export class NoticePage implements OnInit {

  public noticeData:any[]=[];
  private page = 1;
  @ViewChild(IonInfiniteScroll) infiniteScroll: IonInfiniteScroll;
  @ViewChild(IonRefresher) ionRefresher: IonRefresher;
  constructor(private navCtrl:NavController,private http:HttpService,private popWin:PopwinService,private store:Storage) { }

  ngOnInit() {
      this.getListData();
  }
  getListData(){
    this.page=1;
    this.loadData(1,false);
    // let that=this;
    // this.http.get(UrlService.ALL_CONTENT, { "callback": "JSONP_CALLBACK"}).subscribe(res => {
    //   let data = res.json();
    //   ToolService.setEncrypt(data.encrypt);
    //   if (data.code == ConfigService.SUCCESS_CODE) {
    //     let temp = data.data;
    //     let res = ToolService.aesDecodeToJson(temp);
    //     that.noticeData=res;
    //     console.log(res);
    //   } else if (data.code == ConfigService.FAIL_CODE) {
    //     that.popWin.toast(data.message);
    //   } else if (data.code == ConfigService.LOGIN_CODE) {
    //     that.navCtrl.navigateForward("/login");
    //   }
    // }, error => {

    //   console.log("error", error);
    // });
  }
  backClick(){
    this.navCtrl.navigateBack("/");
  }
  /**
   * 跳转到公告的详细
   */
  toDetail(item){
    this.store.set(ConfigService.NOTICEDETAIL,item).then(()=>{
      this.navCtrl.navigateForward("/noticedetail");
    });
  }

  loadData(page, isMore: boolean = false) {
    let that = this;
    let obj = "{\"page\":\"" + page + "\",\"pageSize\":\"35\"}";
    this.http.get(UrlService.ALL_CONTENT, { "callback": "JSONP_CALLBACK", "data": ToolService.aesEncode(obj) }).subscribe(res => {
      let data = res.json();
      ToolService.setEncrypt(data.encrypt);
      that.ionRefresher.complete();
      that.infiniteScroll.complete();
      if (data.code == ConfigService.SUCCESS_CODE) {
        let temp = data.data;
        let tt = ToolService.aesDecodeToJson(temp);
        if (isMore == false && tt.length == 0) {
          that.popWin.toast("暂无开奖结果");
          that.noticeData = [];
          return;
        }
        if (!isMore) {//下拉或第一次加载
          that.noticeData=tt;
          that.infiniteScroll.disabled = false;
        } else {
          if (tt.length > 0) {
            that.noticeData = that.noticeData.concat(tt);
          } else {
            that.infiniteScroll.disabled = true;
            that.popWin.toast("数据已经加载完成");
          }
        }
        console.log(tt);
      } else if (data.code == ConfigService.FAIL_CODE) {
        that.popWin.toast(data.message);
      } else if (data.code == ConfigService.LOGIN_CODE) {
       // that.navCtrl.navigateForward("/login");
      }
    }, error => {
      console.log("error", error);
      that.ionRefresher.complete();
      that.infiniteScroll.complete();
      // if (infinite) {
      //   that.infiniteScroll.complete();
      // }
    });
  }

  doRefresh(event){
      this.getListData();
  }

  loadMoreData(event){
      this.page++;
      this.loadData(this.page,true);
  }











}
